<template>
  <div>
    <el-table :data="tableData" border style="width: 80%">
      <el-table-column fixed prop="id" label="序号" width="150"> </el-table-column>
      <el-table-column fixed prop="date" label="日期" width="150"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="120"> </el-table-column>
      <el-table-column prop="province" label="省份" width="120"> </el-table-column>
      <el-table-column prop="city" label="市区" width="120"> </el-table-column>
      <el-table-column prop="address" label="地址" width="300"> </el-table-column>
      <el-table-column prop="zip" label="邮编" width="120"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
          <el-button type="text" size="small" @click="upDate(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination layout="prev, pager, next" :total="defaultTableData.length" :current-page="current" @current-change="currentChange">
      </el-pagination>
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="40%" destroy-on-close>
      <!-- 父子传值  传递自定义事件 -->
      <TableSon v-bind="{ tablerow }" @onCancel="onCancel" />
    </el-dialog>
    <el-drawer title="修改样式" :visible.sync="drawer" destroy-on-close>
      <UpDate v-bind="{ drawer, tablerow }" @onCancel="drawer = false" />
      <div style="margin: 20px"></div>
    </el-drawer>
  </div>
</template>

<script>
import TableSon from './Son.vue';
import UpDate from './Drawer.vue';
export default {
  components: {
    TableSon,
    UpDate,
  },
  methods: {
    handleClick(row) {
      this.dialogVisible = true;
      this.tablerow = row;
    },
    currentChange(current) {
      const data = [...this.defaultTableData];
      const a = data.splice((current - 1) * 10, 10);
      this.tableData = a;
    },
    onCancel() {
      this.dialogVisible = false;
    },
    upDate(row) {
      this.tablerow = row;
      this.drawer = true;
    },
  },
  mounted() {
    const table_row = JSON.parse(localStorage.getItem('tableRow'));
    const newData = this.defaultTableData.map((item) => {
      if (item.id === table_row?.id) {
        return table_row;
      }
      return item;
    });
    this.defaultTableData = newData;
    this.tableData = [...this.defaultTableData].splice(0, 10);
  },
  data() {
    const item = {
      date: '2016-05-01',
      name: '王小虎',
      province: '上海',
      city: '普陀区',
      address: '上海市普陀区金沙江路 1519 弄',
      zip: 200333,
    };
    return {
      defaultTableData: Array(50)
        .fill(item)
        .map((item, key) => ({ ...item, id: key + 1 })),
      tableData: [],
      current: 1,
      dialogVisible: false,
      tablerow: {},
      drawer: false,
    };
  },
};
</script>

<style></style>
